package com.gaohe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.common.BusinessException;
import com.gaohe.common.CodeUtils;
import com.gaohe.common.RedisCache;
import com.gaohe.common.Result;
import com.gaohe.mapper.UserMapper;
import com.gaohe.pojo.User;
import com.gaohe.pojo.VO.LoginMemberVo;
import com.gaohe.service.UserService;
import com.gaohe.utils.SendCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisCache redisCache;
    @Override
    public Result login(LoginMemberVo loginMemberVo) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        //1.判断登录方式
        String loginType = loginMemberVo.getLoginType();
        if ("username".equals(loginType)) {
            //2.用户名 + 密码
            String username = loginMemberVo.getUsername();
            String password = loginMemberVo.getPassword();
            //3.密码加密
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            //4.用户名 + 密码 查询
            qw.eq(User::getUsername, username)
                    .eq(User::getPassword, password);

        }
        if ("phone".equals(loginType)){
            //1.获取 存入redis中的验证码
            String codeRedis = redisCache.getCacheObject(loginMemberVo.getPhone());
            //2.和用户的输入的比对
            //2.1  验证码有没有过期
            if(Objects.isNull(codeRedis)){
                return Result.error("验证码过期，请重新获取");
            }
            if (!codeRedis.equals(loginMemberVo.getCode())){
                return Result.error("验证码输入错误");
            }
            //3.根据手机号  查
            qw.eq(User::getPhone,loginMemberVo.getPhone());
//            User user = this.getOne(qw);
//            //4.判断 空   用户没有注册
//            if (Objects.isNull(user)){
//                return Result.error("该手机号未注册，请先注册账号");
//            }

        }
        User user = this.getOne(qw);
        //4.判断 空   用户没有注册
        if (Objects.isNull(user)){
            if ("username".equals(loginType)){
                return Result.error("用户名或密码错误");
            }
            if ("phone".equals(loginType)){
                return Result.error("该手机号未注册，请先注册账号");
            }
        }
        //5.状态 == 0 禁用
        if (user.getStatus() == 0){
            return Result.error("账号被禁用");
        }
        //6.存入redis  成功
        redisCache.setCacheObject(
                "user_"+user.getUserId(),
                user,
                1,
                TimeUnit.HOURS
        );
        //7.存redis 登录成功
        return Result.success(user);

    }

    /**
     * 获取验证码
     * @param loginMemberVo
     * @return
     */
    @Override
    public Result getCode(LoginMemberVo loginMemberVo) {
        String code = CodeUtils.generateValidateCode(6);
        //存入缓存
        redisCache.setCacheObject(
                loginMemberVo.getPhone(),
                code,
                5,
                TimeUnit.MINUTES
        );
        //给手机号发送验证码
        try {
            SendCodeUtil.sendCode(loginMemberVo.getPhone(),code);
        } catch (Exception e) {
            new BusinessException("发送失败，改手机号不支持");
        }
        log.info("验证码:{}",code);
        return Result.success();
    }
}
